var $table = $("#stationTab");
$(function () {
    initTable();
    //条件查询
    $("#queryStationBtn").click(function () {
        $table.bootstrapTable('refresh');
    });
    //增加按钮
    $("#addBut").click(function () {
    	initStationEdit(null);
    });
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        showColumns: true,
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../staion/getStationByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "站点编号",
                field: "stationNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "站点名称",
                field: "stationName",
                align: 'center',
                valign: 'middle'

            },
            {
                title: "站点省份",
                field: "stationProvince",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "站点城市",
                field: "stationCity",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "站点地区",
                field: "stationArea",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "站点地址",
                field: "stationAddr",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "站点电话",
                field: "stationPhone",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "插入时间",
                field: "stationInsTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "是否禁用",
                field: "stationIsDelete",
                align: 'center',
                formatter: function(value, row, index){
            		var isdel = "";
            		if(row.stationIsDelete == 1){
            			isdel = '<span style="color:#ed5565;">已禁用</span>';
            		}else if(row.stationIsDelete == 0){
            			isdel ='<span style="color:#23c6c8;">未禁用</span>';
            		}
            		return isdel;
            	}
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var e = '<button class="btn btn-xs btn-info" type="button" onclick="initStationEdit(\'' + row.stationId + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
                    var d = '';
                    if (row.stationIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteStation(\'' + row.stationId + '\',\'' + row.stationName + '\',\'' + 1 + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
                    }else if(row.stationIsDelete == 1){
                        d = '<button class="btn btn-xs btn-warning" type="button" onclick="deleteStation(\'' + row.stationId + '\',\'' + row.stationName + '\',\'' + 0 + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
                    }
                    return e + d;
                }
            }
        ]
    });
}

//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		stationNum: $.trim($('#stationNum').val()),
		stationName: $.trim($('#stationName').val()),
		stationAddr: $.trim($('#stationAddr').val()),
		stationPhone: $.trim($('#stationPhone').val()),
		stationIsDelete: $.trim($('#stationIsDelete').val())
	};
	return temp;
}

//编辑站点信息
function initStationEdit(id){
    var title;
    if(id){
        title = '编辑站点';
    }else{
        title = '新增站点';
    }
    var index = layer.open({
        type: 2,
        title: title,
        maxmin: false,	//禁止使用最大化按钮
        shadeClose: false, //点击遮罩关闭层
        area : ['500px' , '750px'],
        content: '../station/edit_station.htm?id='+id,
        success: function(layero, index){
        }
    });
    layer.full(index);
}

//设置站点状态
function deleteStation(stationId, stationName, isDelete) {
    layer.confirm('你确定要修改【' + stationName + '】站点状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../station/modifyStationDisable.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'stationId': stationId,'isDel':isDelete},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../staion/getStationByPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}